/*
 * @Author: your name
 * @Date: 2024-05-05 17:05:20
 * @LastEditTime: 2024-05-05 17:08:35
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\538.把二叉搜索树转换为累加树.cpp
 */
/*
 * @lc app=leetcode.cn id=538 lang=cpp
 *
 * [538] 把二叉搜索树转换为累加树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int pre = 0;
    void traversal(TreeNode* cur) { // 右中左遍历
        if(cur == nullptr) return ;
        traversal(cur->right);
        cur->val += pre;
        pre = cur->val;
        traversal(cur->left);
    }
    TreeNode* convertBST(TreeNode* root) {
        pre = 0;traversal(root);
        return root;

    }
};
// @lc code=end

